package com.hzw.saas.api.storage.enums;

import java.util.Objects;

import cn.hutool.core.lang.Assert;

/**
 * 文件完整性状态
 *
 * @author zzl
 * @since 02/26/2021
 */
public enum FileIntegrityEnum {

    NORMAL(200, "正常"),
    UPLOADING(201, "上传中"),
    MERGING(202, "合并中"),
    ;
    public static final String API_TEXT = "200-正常, 201-上传中, 202-合并中";

    private final Integer code;
    private final String text;

    FileIntegrityEnum(Integer code, String text) {
        this.code = code;
        this.text = text;
    }

    public Integer getCode() {
        return code;
    }

    public String getText() {
        return text;
    }

    public static String getText(Integer code) {
        for (FileIntegrityEnum value : FileIntegrityEnum.values()) {
            if (Objects.equals(value.code, code))
                return value.text;
        }
        return "";
    }

    public static boolean isValid(Integer code) {
        FileIntegrityEnum instance = instance(code);
        return Objects.nonNull(instance);
    }

    public static void assertValid(Integer code) {
        FileIntegrityEnum instance = instance(code);
        Assert.notNull(instance, "文件完整性编号异常");
    }

    public static FileIntegrityEnum instance(Integer code) {
        for (FileIntegrityEnum value : FileIntegrityEnum.values()) {
            if (Objects.equals(value.code, code))
                return value;
        }
        return null;
    }
}
